//
//  CWG_ViewControllerForDengLu.m
//  ProjectB_
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 项目B. All rights reserved.
//

#import "CWG_ViewControllerForDengLu.h"

#import <AVOSCloudSNS.h>

@interface CWG_ViewControllerForDengLu ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textFieldOfNumber;    //账号输入框
@property (weak, nonatomic) IBOutlet UITextField *textFieldOfSecret;    //密码输入框
@property (weak, nonatomic) IBOutlet UIButton *buttonForWeiXin; //微信第三方登录button
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *phoneConstraint;   // 约束
@property (nonatomic, assign) BOOL isBegin;

@end

@implementation CWG_ViewControllerForDengLu
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationController.navigationBarHidden = YES;
    
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor lightGrayColor], NSFontAttributeName:[UIFont systemFontOfSize:14]};
    self.textFieldOfNumber.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"点击输入手机号登录" attributes:dic];
    self.textFieldOfNumber.tintColor = [UIColor whiteColor];
    self.textFieldOfSecret.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入密码" attributes:dic];
    self.textFieldOfSecret.tintColor = [UIColor whiteColor];
    
    // 添加将要显示的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    // 添加将要隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
  
    
}

#pragma mark - 键盘将要显示
- (void)keyboardWillShow:(NSNotification *)noti
{
    // 监听方法会执行多次, 通过BOOL值筛选掉重复的执行
    if (!_isBegin) {
        // 修改约束的优先级为 751
        [self textFieldConstraint:self.phoneConstraint priority:751 fromView:self.view];
        _isBegin ^= 1;
    }
}

#pragma mark - 键盘将要隐藏
- (void)keyboardWillHiden:(NSNotification *)noti
{
    // 如果isBegin为Yes
    // 说明键盘已经弹出
    if (_isBegin) {
        // 修改约束的优先级 500
        [self textFieldConstraint:self.phoneConstraint priority:500 fromView:self.view];
        _isBegin ^= 1;
    }
}

#pragma mark - 修改优先级方法
- (void)textFieldConstraint:(NSLayoutConstraint *)constraint priority:(float)priority fromView:(UIView *)view
{
    constraint.priority = priority;
    [UIView animateWithDuration:1.5f animations:^{
        [view layoutIfNeeded];
    }];
    
}

- (IBAction)weiXinForAction:(UIButton *)sender {
}

// 微博第三方登录
- (IBAction)weiBoForAction:(UIButton *)sender {
    
    [AVOSCloudSNS setupPlatform:AVOSCloudSNSSinaWeibo withAppKey:@"1955798598" andAppSecret:@"f494337ab7cf717915e843b729184a75" andRedirectURI:@"https://api.weibo.com/oauth2/default.html"];
    
    [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
        if (error) {
            NSLog(@"%@", error);
        } else {
//            NSString *accessToken = object[@"access_token"];
//            NSString *username = object[@"username"];
//            NSString *avatar = object[@"avatar"];

            NSDictionary *rawUser = object[@"raw-user"]; // 性别等第三方平台返回的用户信息
            
            NSLog(@"%@", [rawUser objectForKey:@"profile_image_url"]);
            
            NSLog(@"%@", [rawUser objectForKey:@"name"]);
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[rawUser objectForKey:@"profile_image_url"] forKey:@"userImage"];
            [dic setValue:[rawUser objectForKey:@"name"] forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"user"];
            
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"是否登录"];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [self.navigationController popViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = NO;
            
        }
    } toPlatform:AVOSCloudSNSSinaWeibo];
}

- (IBAction)goBackAction:(id)sender {
    NSLog(@"推出");
    
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
   // [[NSNotificationCenter defaultCenter] postNotificationName:@"显示" object:nil];
   // self.navigationController.navigationBar.hidden = NO;
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
